// pages/goods/search/index.js
var app = getApp();
Page({
    /**
     * 页面的初始数据
     */
    data: {
        parameter: {
            'navbar': '1',
            'return': '1',
            'title': '铺货搜索',
            'color': true,
            'class': false
        },
        executorUserId: "",
        historyList: wx.getStorageSync('historyList') || [],
        store: null,
        inputValue: "",
        goodsList: [],
        await: 'none',
        pic: "https://pic.localhost.com/",
        showHistory: true,
        isSearchList: true,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    // onLoad: function (options) {
        
    // },
    onShow: function (option) {
        var that = this;
        wx.getStorage({
            key: 'userInfo',
            success(res) {
                that.setData({
                    executorUserId: res.data.id,
                    historyList: wx.getStorageSync('historyList') || []
                })
                if (that.data.inputValue) {
                    that.search();
                } else if (that.data.historyId) {
                    that.history(that.data.historyId)
                }
            }
        })
    },
    // 读取输入值
    inputText: function (e) {
        this.setData({
            inputValue: e.detail.value
        })
    },
    //把用户输入的值保存在inputValue中
    doSearch: function (e) {
        var inputValue = this.data.inputValue;
        var historyList = this.data.historyList || [];
        if (inputValue) {
            historyList.push(inputValue);
            wx.setStorageSync('historyList', historyList);
        }
        this.search()
    },
    //搜索值
    search() {
        let that = this;
        var inputValue = this.data.inputValue;
        app.baseGet(app.U({
            m: 'api',
            c: 'task',
            a: 'worker/distribution/getTaskOrderByName',
            q: {
                goodsName: inputValue, //店铺名称
                userId: that.data.executorUserId
            }
        }), res => {
            that.setData({
                store: res.data,
            })
        }, (res) => {
            that.setData({
                store: null,
                historyList: wx.getStorageSync('historyList')
            })
        })
    },
    //历史搜索
    history(e) {
        var that = this;
        app.baseGet(app.U({
            m: 'api',
            c: 'task',
            a: 'worker/distribution/getTaskOrderByName',
            q: {
                goodsName: e.currentTarget.dataset.item, //店铺名称
                userId: that.data.executorUserId
            }
        }), res => {
            that.setData({
                store: res.data,
                historyId: e
            })
        }, (res) => {
            that.setData({
                store: null,
                historyList: wx.getStorageSync('historyList')
            })
        })
    },
    // 隐藏搜索历史框
    hideHistory: function () {
        this.setData({
            showHistory: !this.data.showHistory
        });
    },
    // 显示搜索历史框
    showHistory: function () {
        this.setData({
            showHistory: !this.data.showHistory,
            searchRecord: wx.getStorageSync('historyList')
        })
    },
    //折叠面板
    isOpen: function (e) {
        var that = this;
        var idx = e.currentTarget.dataset.index;
        that.setData({
            ['store.[' + idx + '].hidden']: that.data.store[idx].hidden ? false : true
        })
    },
    clearHistory: function () {
        wx.removeStorageSync('historyList')
        this.setData({
            historyList: []
        })
    },
    //拒绝订单
    refuse(e) {
        var that = this;
        app.baseGet(app.U({
            m: 'api',
            c: 'task',
            a: 'worker/distribution/refuseTaskOrder',
            q: {
                goodsId: e.currentTarget.dataset.goodsid,
                dealerId: e.currentTarget.dataset.dealerid,
                userId: that.data.executorUserId
            }
        }), res => {
            that.cancle();
            if (this.data.inputValue) {
                that.search();
            } else {
                that.history(that.data.historyId)
            }
        })
    },
    //订单详情
    goodsInfo(e) {
        var that = this;
        app.baseGet(app.U({
            m: 'api',
            c: 'task',
            a: 'worker/distribution/getOrder',
            q: {
                goodsId: e.currentTarget.dataset.goodsid,
                dealerId: e.currentTarget.dataset.dealerid,
                userId: that.data.executorUserId
            }
        }), res => {
            that.setData({
                orderinfo: res.data,
                status: res.data.status,
                dealer: res.data.dealer,
                goods: res.data.goods
            })
            that.cancle();
        }, (res) => {})
    },
    // 显示隐藏弹框
    cancle: function () {
        this.setData({
            await: this.data.await == "none" ? "block" : 'none'
        })
    },
    // 跳转铺货
    task: function (e) {
        wx.navigateTo({
            url: "/pages/goods/task/index?dealerId=" + JSON.stringify(e.currentTarget.dataset.dealerid) + "&goodsId=" + JSON.stringify(e.currentTarget.dataset.goodsid)
        })
    },
    // 跳转订单信息
    taskInfo: function (e) {
        wx.navigateTo({
            url: "/pages/goods/taskInfo/index?dealerId=" + JSON.stringify(e.currentTarget.dataset.dealerid) + "&goodsId=" + JSON.stringify(e.currentTarget.dataset.goodsid)
        })
    },
})